/**
 * @Author: 陈瑞鹏
 * @Date:   2022-12-05 17:39:02
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2022-12-05 17:45:46
 */

import BaseGameComponent from "../../libs/core/BaseGameComponent";
import { redPointKey, redPointCalKey } from "./redPointEnum";
import { INotification } from "../../libs/pureMvc/Interfaces";
import { redPointCtrl } from "./redPointCtrl";
import { PureMVCEvents } from "../core/PureMVCEvents";

const { ccclass, property } = cc._decorator;

@ccclass
export default class redPointItem extends BaseGameComponent {

    //红点条件key值
    @property({ type: [cc.Enum(redPointKey)], tooltip: "红点条件key值" })
    public keyArr: Array<redPointKey> = [];

    //红点计算key值
    @property({ type: [cc.Enum(redPointCalKey)], tooltip: "红点计算key值" })
    public CalkeyArr: Array<redPointCalKey> = [];

    @property
    public removeSelfTime: number = 0;

    private redSprite: cc.Node = null;

    public onLoad(): void {
        this.redSprite = this.node.getChildByName("redSprite");
        if (!this.redSprite) this.redSprite = this.node;
    }

    public onEnable(): void {
        super.onEnable();

        this.updateUI();
    }

    public updateUI() {
        for (let v in this.keyArr) {
            let _key = this.keyArr[v];
            let _is = redPointCtrl.getInstance().isShow(_key) && redPointCtrl.getInstance().isEnableCalRed();

            // console.log(redPointCtrl.getInstance().isShow(_key), _key, "redPointCtrl.getInstance().isShow(_key)")
            if (_is) {
                this.redSprite.active = true;

                if (this.removeSelfTime) {

                    this.redSprite.active = false;
                    // let that: redPointItem = this;
                    // this.redSprite.runAction(cc.sequence(cc.delayTime(this.removeSelfTime),cc.callFunc(function ()
                    // {
                    //     that.redSprite.active = false;
                    // })));
                }

                return;
            }
        }
        this.redSprite.active = false;

    }

    /** pureMVC 消息监听列表 */
    public listNotificationInterests(): string[] {
        return [PureMVCEvents.UPDATE_RED_POINT];
    }

    /**
     * pureMVC消息监听处理函数
     * @notification 消息体
     */
    public handleNotification(notification: INotification): void {
        let evtName = notification.getName();
        let body = notification.getBody();
        // console.log(evtName, body, "handleNotification");
        switch (evtName) {
            case PureMVCEvents.UPDATE_RED_POINT:
                let key: redPointCalKey = body.key;
                let isshow = body.isshow;
                // console.log(key, "handleNotification")
                for (let v in this.CalkeyArr) {
                    if (this.CalkeyArr[v] == key) {
                        this.updateUI();
                        return;
                    }
                }
                break;
        }
    }

    isShow(bool: boolean) {

        if (this.redSprite) {
            this.redSprite.active = bool;
        }

    }

}
